/*
 * @(#)CollectionPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JTable;

import com.netexplode.jtunes.model.media.CollectionManager;
import com.netexplode.jtunes.model.player.PlayerException;
import com.netexplode.jtunes.model.player.PlayerManager;

/**
 * <code>CollectionPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.1 $
 * @since 0.1
 */
public abstract class CollectionPanel extends JPanel {

    protected boolean collectionLoaded;

    protected JTable[] tables;

    protected final String[] columnNames = {
            "artist",
            "album",
            "title" };

    protected CollectionPanel() {
        super();
        initialize();
    }

    abstract protected void initialize();

    protected void addEventLister(final JTable tbl) {
        tbl.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if ((e.getButton() == MouseEvent.BUTTON1)
                        && (e.getClickCount() == 2)) {
                    Point point = e.getPoint();

                    int row = tbl.rowAtPoint(point);

                    CollectionManager
                            .getInstance()
                            .getCurrrentCollection()
                            .setPosition(row);

                    try {
                        PlayerManager.getInstance().getMediaPlayer().stop();
                        PlayerManager.getInstance().getMediaPlayer().play();
                    }
                    catch (PlayerException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        });
    }

    protected void refresh() {
        if (!collectionLoaded) {
            this.initialize();
        }
    }

    abstract protected JTable getTable();
}